layui.use(['form', 'layer'], function () {
    const form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    // 加载
    $.ajax({
        url: $.cookie("tempUrl") + "hSysDoctorAppointmentAllocation/findById?id=" + $.cookie("tempId"),
        type: "GET",
        success: function (result) {
            if (result.code === 200) {
                $(".startTime").val(result.data.startTime);
                $(".endTime").val(result.data.endTime);
                $(".interrogationStandard").val(result.data.interrogationStandard);
                //加载周期范围
                let strArray = result.data.type.split(',');
                for (let index in strArray) {
                    switch (strArray[index]) {
                        case '1':
                            $(".type1").attr("checked", true);
                            break;
                        case '2':
                            $(".type2").attr("checked", true);
                            break;
                        case '3':
                            $(".type3").attr("checked", true);
                            break;
                        case '4':
                            $(".type4").attr("checked", true);
                            break;
                        case '5':
                            $(".type5").attr("checked", true);
                            break;
                        case '6':
                            $(".type6").attr("checked", true);
                            break;
                        case '7':
                            $(".type7").attr("checked", true);
                            break;
                    }
                }
                form.render();
            } else {
                layer.msg('查询异常', {icon: 7, anim: 6});
            }
        }
    });

    form.verify({
        startTime: function (value, item) { //value：表单的值、item：表单的DOM对象
            if (value === '') {
                return '请输入开始时间';
            }
        },
        endTime: function (value, item) { //value：表单的值、item：表单的DOM对象
            if (value === '') {
                return '请输入结束时间';
            }
            if (parseInt(value) <= parseInt(form.val("formTest").startTime)) {
                return '结束时间需要大于开始时间';
            }
        }
    });

    //点击取消按钮事件
    $(".cancelNews").click(function () {
        parent.layer.closeAll("iframe")
    });

    //点击确定按钮事件
    form.on("submit(addNews)", function (data) {
        let type = "";
        if (data.field.type1 !== undefined) {
            type += "1,"
        }
        if (data.field.type2 !== undefined) {
            type += "2,"
        }
        if (data.field.type3 !== undefined) {
            type += "3,"
        }
        if (data.field.type4 !== undefined) {
            type += "4,"
        }
        if (data.field.type5 !== undefined) {
            type += "5,"
        }
        if (data.field.type6 !== undefined) {
            type += "6,"
        }
        if (data.field.type7 !== undefined) {
            type += "7,"
        }
        layer.confirm('提交后系统会进行审核，预计审核时间0.5天，请及时关注短信', {area: ["240px", "190px"], title: '系统提示'}, function (index0) {
            //do something
            $.ajax({
                url: $.cookie("tempUrl") + "hSysDoctorAppointmentAllocation/updateById",
                type: "PUT",
                datatype: "application/json",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    id: $.cookie("tempId"),
                    doctorId: $.cookie("doctorId"),
                    startTime: data.field.startTime,
                    endTime: data.field.endTime,
                    type: type,
                    cf: 1,
                    state: 1,
                    interrogationStandard: data.field.interrogationStandard
                }),
                success: function (result) {
                    if (result.code === 200) {
                        layer.msg("更新成功");
                        setTimeout(function () {
                            // top.layer.msg("新增成功2");
                            layer.closeAll("iframe");
                            //刷新父页面
                            parent.location.reload();
                        }, 500);
                    } else {
                        layer.msg(result.exception, {icon: 7, anim: 6});
                    }
                }
            });
            // layer.close(index);
        });

        return false;
    });
});